﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public class TinhTrangDAO : DataProvider
    {
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            TinhTrangDTO objectDTO = new TinhTrangDTO();
            objectDTO.MaTinhTrang = int.Parse(dt.Rows[i]["MaTinhTrang"].ToString());
            objectDTO.TenTinhTrang = dt.Rows[i]["TenTinhTrang"].ToString();
            objectDTO.TenGhiChu = dt.Rows[i]["GhiChu"].ToString();
            return (object)objectDTO;
        }

        public TinhTrangDTO[] GetAll()
        {
            String strSQL = "Select * from TinhTrang ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);

            TinhTrangDTO[] TinhTrangDTOs = new TinhTrangDTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                TinhTrangDTOs[i] = (TinhTrangDTO)GetDataFromDataRow(dt, i);

            return TinhTrangDTOs;
        }

        public void insert(TinhTrangDTO info)
        {
            string insertCommand = "INSERT TinhTrang VALUES(" +
                info.MaTinhTrang + ", '" +
                info.TenTinhTrang + "', " +
                info.TenGhiChu + ")";

            ExecuteNonQuery(insertCommand);
        }

        public void update(TinhTrangDTO info)
        {
            string updateCommand = "UPDATE TinhTrang " +
                                    "SET TenTinhTrang = '" + info.TenTinhTrang + "', " +
                                    " GhiChu = '" + info.TenGhiChu +
                                    " WHERE MaTinhTrang = " + info.MaTinhTrang;


            ExecuteNonQuery(updateCommand);
        }

        public void delete(string maTinhTrang)
        {
            string deleteCommand = "DELETE FROM TinhTrang WHERE MaTinhTrang =" + maTinhTrang;
            ExecuteNonQuery(deleteCommand);
        }
    }
}
